package cn.com.mdx.sort.comparator;

import java.util.Arrays;
import java.util.Comparator;

/**
 * <p> 学生类
 *
 * @author ：huangy
 * @date ：Created in 2020/9/17 10:07 上午
 */
public class Student implements Comparable<Student> {

    //自定义比较器
    /**
     * 通过名称比较
     */
    public static final Comparator<Student> BY_NAME = new ByName();

    /**
     * 通过年龄比较
     */
    private static final Comparator<Student> BY_AGE = new ByAge();

    /**
     * 姓名
     */
    private String name;

    /**
     * 年龄
     */
    private Integer age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student that) {
        //校验当前学生信息是否合法
        if (checkStudentEmpty(this)) return -1;
        //校验被比较的学生信息是否合法
        if (checkStudentEmpty(that)) return 1;
        //以姓名为优先排序
        if (this.name.compareTo(that.name) > 0) return 1;
            //姓名如果相同就通过年龄比较
        else if (this.name.compareTo(that.name) == 0)
            return this.age.compareTo(that.age);
        return -1;
    }

    public String toString() {
        return "[姓名：" + name + ", 年龄：" + age + "]";
    }

    /**
     * @param student 要校验的学生
     * @return boolean
     * @Author huangy
     * @description //TODO 校验学生数据是否合法
     * @date 11:04 上午 2020/9/17
     **/
    private boolean checkStudentEmpty(Student student) {
        return student.name == null || student.name.equals("") || student.age == null;
    }

    /**
     * @Author huangy
     * @description //TODO 名称比较器，通过名称比较
     * @date 2:03 下午 2020/9/17
     * @return
     **/
    private static class ByName implements Comparator<Student> {
        @Override
        public int compare(Student one, Student other) {
            return one.name.compareTo(other.name);
        }
    }

    /**
     * @Author huangy
     * @description //TODO 名称比较器，通过年龄比较
     * @date 2:03 下午 2020/9/17
     * @return
     **/
    private static class ByAge implements Comparator<Student> {
        @Override
        public int compare(Student one, Student other) {
            return one.age.compareTo(other.age);
        }
    }

    public static void main(String[] args) {
        Student s1 = new Student("s1", 25);
        Student s2 = new Student("s1", 21);
        Student s3 = new Student("s2", 18);
        Student s4 = new Student("s2", 19);
        Student s5 = new Student("s2", 12);
        Student s6 = new Student("s3", 46);
        Student[] students = new Student[]{s1, s2, s3, s4, s5, s6};
        printStudent(students);
        Arrays.sort(students, Student.BY_NAME);
        printStudent(students);
        Arrays.sort(students, Student.BY_AGE);
        printStudent(students);
    }

    /**
     * @param students 学生数组
     */
    public static void printStudent(Student[] students) {
        if (students != null && students.length > 0) {
            for (int i = 0; i < students.length; i++)
                System.out.println("第" + (i + 1) + "名是：" + students[i].toString());
        }
        System.out.println("-----------------------------------");
    }

}
